package com.bw.module_store.view

import android.graphics.Color
import android.text.Editable
import android.text.TextWatcher
import android.view.View
import android.widget.PopupMenu
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.ToastUtils
import com.bw.lib_base.path.RouterPath
import com.bw.lib_common.view.BaseMvvmActivity
import com.bw.module_store.R
import com.bw.module_store.adapter.StoreAdapter
import com.bw.module_store.databinding.ActivityStoreBinding
import com.bw.module_store.viewmodel.StoreViewModel
/**
 * 智慧园区-01-商家推荐-v1.1-吴熙-20250511
 */
@Route(path = RouterPath.PATH_STORE_ACTIVITY)
class StoreActivity : BaseMvvmActivity<ActivityStoreBinding>() {

    lateinit var storeAdapter: StoreAdapter
    private val viewModel : StoreViewModel by viewModels()


    override fun initView() {
        //头布局
        mDataBinding.goBack.visibility=View.GONE
        mDataBinding.goBackBlack.visibility=View.VISIBLE
        mDataBinding.title.text="企业档案"
        mDataBinding.title.setTextColor(Color.BLACK)
        mDataBinding.topbarStore.setBackgroundColor(Color.WHITE)
        mDataBinding.goAdd.visibility=View.GONE
        mDataBinding.goBackBlack.setOnClickListener { finish() }
        mDataBinding.goMoreBlack.visibility=View.VISIBLE

        //popMenu弹出菜单
        mDataBinding.goMoreBlack.setOnClickListener {
            //创建PopMenu
            val popupMenu= PopupMenu(this,it)
            //加载菜单资源
            popupMenu.menuInflater.inflate(R.menu.menu_store,popupMenu.menu)
            //设置菜单的监听事件
            popupMenu.setOnMenuItemClickListener {
                when(it.itemId){
                    R.id.store1->{
                        //跳转到申请入驻
                        ARouter.getInstance().build("/addStore/addStore").navigation()
                        true
                    }
                    R.id.store2->{
                        //跳转到入驻审批
                        ARouter.getInstance().build("/approveStore/approveStore").navigation()
                        true
                    }
                    else -> true
                }
            }
            popupMenu.show()
        }


        //列表
        storeAdapter=StoreAdapter()
        mDataBinding.rv.layoutManager=LinearLayoutManager(this)
        mDataBinding.rv.adapter=storeAdapter
        viewModel.getStoreList()

        //点击列表跳转到商家详情
        storeAdapter.setOnItemClickListener { adapter, view, position ->
            ARouter.getInstance().build("/storeDetail/storeDetail")
                .withInt("storeId",storeAdapter.getItem(position).id)
                .navigation()
        }

        //监听搜索框
        mDataBinding.edSearch.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable?) {
                if (s.toString().length>0){
                    searchStore(s.toString())
                }else{
                    viewModel.getStoreList()
                }
            }
        })
    }

    private fun searchStore(keyword: String) {
        val iterator = storeAdapter.data.iterator()
        while (iterator.hasNext()) {
            val store = iterator.next()
            if (!store.name.contains(keyword) && !store.legalRepresentative.contains(keyword)) {
                iterator.remove() // 删除
                storeAdapter.notifyDataSetChanged()

                if (storeAdapter.data.size==0){
                    ToastUtils.showShort("未搜索到${keyword}")
                    return
                }
            }
        }
    }

    override fun observeDataChange(){
        viewModel.getStoreListSuccess.observe(this){
            storeAdapter.data.clear()
            storeAdapter.data.addAll(it)
            storeAdapter.notifyDataSetChanged()
        }
    }

    override fun getLayoutId(): Int =R.layout.activity_store

}